#include "coin.h"

Coin::Coin(QWidget *parent) : QPushButton(parent){

}
Coin::Coin(int x){
    this->n = x;
    timer = new QTimer(this);
//    初始化金币按钮图标
    QPixmap pix;
    if(this->n==1){
        pix.load(":/resource/Coin0001.png");
    } else if(this->n==0){
        pix.load(":/resource/Coin0008.png");
    }else{
        qDebug() << "加载金币图片失败";
        return;
    }
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setFixedSize(pix.width(),pix.height());
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));


    connect(timer,&QTimer::timeout,[=](){
        //这两个参数已放入头文件中
//        QPixmap pic;
//        QString path;

        //这里的判断要反过来 因为在flip()中已经修改了n的值
        if(this->n==0){
            path = QString(":/resource/Coin000%1.png").arg(min++);
        }else{
            path = QString(":/resource/Coin000%1.png").arg(max--);
        }
        pic.load(path);
        this->setFixedSize(pic.width(),pic.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pic);
        this->setIconSize(QSize(pic.width(),pic.height()));

        if(this->n==0 && max<min){
            this->min=1;
            isAnimation=false;
            timer->stop();
        }else if(this->n==1 && min>max){
            this->max=8;
            isAnimation=false;
            timer->stop();
        }

    });
}

//反转金币
void Coin::flip(){

    if(this->n==1){
        timer->start(30);
        isAnimation=true;
        this->n=0;

    } else if(this->n==0){
        timer->start(30);
        isAnimation=true;
        this->n=1;
    }
}


void Coin::mousePressEvent(QMouseEvent * e){
    if(isAnimation || !Flipable){
        return;
    }else{
        return QPushButton::mousePressEvent(e);
    }
}
